import fs from 'fs-extra';
import path from 'path';

import { buildAppAndHapsAndCheckFile, commonJson5Operation } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  IsEnable,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
  SignType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOption } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },
};
const taskConfig: TestTaskOption = {
  caseLevel: CaseLevel.L1,
  sign: SignType.NoSign,
  ohpmInstall: IsEnable.DisAble
};

const removeBuild = (rootPath: string, entryPath: string) => {
  // 清除产物 避免影响用例继续执行
  return Promise.all([
    fs.remove(path.resolve(rootPath, 'build')), fs.remove(path.resolve(entryPath, 'build')),
  ]);
};

const testFun: TestCaseFun = async (project, taskConfig, record) => {
  const rootPath = project.projectRootPath();
  const entryPath = project.getModule('entry').moduleRootPath();

  // 勾选所有hasMusicNMscList
  fs.cpSync(path.resolve('test', 'resources', 'insightintents', 'hasMusicNMscList', 'IntentExecutorImpl.ets'),
    path.resolve(entryPath, 'src', 'main', 'ets', 'insightintents', 'IntentExecutorImpl.ets'))
  fs.cpSync(path.resolve('test', 'resources', 'insightintents', 'hasMusicNMscList', 'insight_intent.json'),
    path.resolve(entryPath, 'src', 'main', 'resources', 'base', 'profile', 'insight_intent.json'))

  await buildAppAndHapsAndCheckFile(project, taskConfig, record);
  const entryInsightintentsPath = path.resolve(entryPath, 'build', 'default', 'cache', 'default', 'default@CompileArkTS',
    'esmodule', 'debug', 'entry', 'src', 'main', 'ets', 'insightintents', 'IntentExecutorImpl.protoBin');
  const entryInsightintentJsonPath = path.resolve(entryPath, 'build', 'default', 'intermediates', 'res', 'default',
    'resources', 'base', 'profile', 'insight_intent.json');
  expect(await fs.pathExists(entryInsightintentsPath)).toBe(true);
  expect(await fs.pathExists(entryInsightintentJsonPath)).toBe(true);
  await removeBuild(rootPath, entryPath);

  // 仅勾选PlayMusic
  fs.cpSync(path.resolve('test', 'resources', 'insightintents', 'hasMusicNoMscList', 'IntentExecutorImpl.ets'),
    path.resolve(entryPath, 'src', 'main', 'ets', 'insightintents', 'IntentExecutorImpl.ets'))
  fs.cpSync(path.resolve('test', 'resources', 'insightintents', 'hasMusicNoMscList', 'insight_intent.json'),
    path.resolve(entryPath, 'src', 'main', 'resources', 'base', 'profile', 'insight_intent.json'))

  await buildAppAndHapsAndCheckFile(project, taskConfig, record);
  expect(await fs.pathExists(entryInsightintentsPath)).toBe(true);
  expect(await fs.pathExists(entryInsightintentJsonPath)).toBe(true);
  await removeBuild(rootPath, entryPath);

  // 仅勾选PlayMusicList
  fs.cpSync(path.resolve('test', 'resources', 'insightintents', 'noMusicHasMscList', 'IntentExecutorImpl.ets'),
    path.resolve(entryPath, 'src', 'main', 'ets', 'insightintents', 'IntentExecutorImpl.ets'))
  fs.cpSync(path.resolve('test', 'resources', 'insightintents', 'noMusicHasMscList', 'insight_intent.json'),
    path.resolve(entryPath, 'src', 'main', 'resources', 'base', 'profile', 'insight_intent.json'))

  await buildAppAndHapsAndCheckFile(project, taskConfig, record);
  expect(await fs.pathExists(entryInsightintentsPath)).toBe(true);
  expect(await fs.pathExists(entryInsightintentJsonPath)).toBe(true);
  await removeBuild(rootPath, entryPath);
};

TestCaseBuilder.builder()
  .caseName('OPEN_LAB_ZOS_BUILD_HAP_INTENT_FRAMEWORK_001_002')
  .description('001、002、empty工程添加意图框架模板构建')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(taskConfig)
  .testFun(testFun)
  .load();
